package com.pds.web.testOrganizationInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.core.domain.entity.SysDept;
import com.pds.common.core.domain.entity.SysDictData;
import com.pds.common.core.domain.entity.SysRole;
import com.pds.common.core.domain.entity.SysUser;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.common.utils.StringUtils;
import com.pds.system.domain.SysUserRole;
import com.pds.system.mapper.*;
import com.pds.utils.IdCardUtil;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationPerson;
import com.pds.web.testOrganizationInfo.excel.OrganizationPersonExcel;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationInfoMapper;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationPersonMapper;
//import com.pds.web.testOrganizationInfo.service.IPdsTestOrganizationInfoService;
import com.pds.web.testOrganizationInfo.service.IPdsTestOrganizationPersonService;
import com.pds.web.testOrganizationInfo.vo.PdsTestOrganizationPersonVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class PdsTestOrganizationPersonServiceImpl extends ServiceImpl<PdsTestOrganizationPersonMapper, PdsTestOrganizationPerson> implements IPdsTestOrganizationPersonService {

    @Resource
    private PdsTestOrganizationPersonMapper pdsTestOrganizationPersonMapper;
    @Resource
    private PdsTestOrganizationInfoMapper pdsTestOrganizationInfoMapper;
    @Resource
    private SysDictDataMapper dictDataMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysConfigMapper sysConfigMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 机构管理员角色权限字符串
     */
    private static final String ORG_ADMIN_ROLE_KEY = "agencyAdmin";

    /**
     * 新增
     *
     * @param pdsTestOrganizationPerson
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public String addPerson(PdsTestOrganizationPerson pdsTestOrganizationPerson) throws Exception {

        //1. 根据userId查询Info表
//        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        QueryWrapper<PdsTestOrganizationInfo> qw = new QueryWrapper<>();
        PdsTestOrganizationInfo pdsTestOrganizationInfo = pdsTestOrganizationInfoMapper.selectOne(qw.eq("dept_id", deptId));

        //2. 补充pdsTestOrganizationPerson对象的相关属性
        pdsTestOrganizationPerson.setOrganizationId(pdsTestOrganizationInfo.getId());

        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsTestOrganizationPerson.setCreateTime(date);

        pdsTestOrganizationPerson.setStatus("0");

        pdsTestOrganizationPerson.setDeptId(deptId);

        if (StringUtils.isEmpty(pdsTestOrganizationPerson.getPersonName())) {
            throw new Exception("姓名不能为空！");
        }

        if (StringUtils.isEmpty(pdsTestOrganizationPerson.getPhone())) {
            throw new Exception("手机号不能为空！");
        }
        if(!IdCardUtil.check(pdsTestOrganizationPerson.getPersonCode())){
            throw new Exception("身份证号不正确，请核实");
        }

        if (sysUserMapper.selectUserByUserName(pdsTestOrganizationPerson.getPhone()) != null) {
            throw new Exception("账号已存在，注册失败！");
        }

        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        sysUser.setUserName(pdsTestOrganizationPerson.getPhone());
        sysUser.setNickName(pdsTestOrganizationPerson.getPersonName());
        sysUser.setEmail(pdsTestOrganizationPerson.getEmail());
        sysUser.setPhonenumber(pdsTestOrganizationPerson.getPhone());
        sysUser.setSex(pdsTestOrganizationPerson.getSex());
        sysUser.setPassword(SecurityUtils.encryptPassword(sysConfigMapper.selectInitPassword()));
        sysUser.setCreateTime(date);
        sysUser.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        sysUserMapper.insertUser(sysUser);

        //3. 将pdsTestOrganizationPerson对象插入Person表
        pdsTestOrganizationPerson.setUserId(sysUser.getUserId());
        pdsTestOrganizationPersonMapper.addPerson(pdsTestOrganizationPerson);
        statisticianInformation(pdsTestOrganizationInfo, pdsTestOrganizationPerson);

        String personCategory = pdsTestOrganizationPerson.getPersonCategory();
        String[] personCategoryArray = personCategory.split(",");
        List<SysUserRole> userRoleList = new ArrayList<>();
        for (String category : personCategoryArray) {
//            String dictLabel = dictDataMapper.selectDictLabel("person_category", dictVale);
//            Long roleId = sysRoleMapper.getPersonRoleId(dictLabel);
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(sysUser.getUserId());
            userRole.setRoleId(Long.parseLong(category));
            userRoleList.add(userRole);
        }
        sysUserRoleMapper.batchUserRole(userRoleList);

        return "人员信息与账号添加成功！";

//        return pdsTestOrganizationPerson;

    }

    /**
     * 删除 -- 根据id
     *
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean deletePerson(Long id) throws Exception {
        //1. 备份要删除的person数据
        PdsTestOrganizationPerson pdsTestOrganizationPerson = pdsTestOrganizationPersonMapper.selectByPersonId(id);

        //2. 删除person数据
        boolean flag1 = pdsTestOrganizationPersonMapper.deletePerson(id) > 0;

        boolean flag2 = sysUserMapper.deleteUserById(pdsTestOrganizationPerson.getUserId()) > 0;

        boolean flag3 = sysUserRoleMapper.deleteUserRoleByUserId(pdsTestOrganizationPerson.getUserId()) > 0;

        //3. 查询删除的person数据对应的info数据
        PdsTestOrganizationInfo pdsTestOrganizationInfo = pdsTestOrganizationInfoMapper.selectByOrganizationId(pdsTestOrganizationPerson.getOrganizationId());

        //4. Info表与Person表关联信息处理
        PdsTestOrganizationInfo info = new PdsTestOrganizationInfo();
        info.setId(pdsTestOrganizationInfo.getId());

        //4.1 修改Info表的总人数
        String totalPerson = pdsTestOrganizationInfo.getTotalPerson();
        int totalIndex = totalPerson.indexOf("人");
        String oldTotal = totalPerson.substring(0, totalIndex);
        BigDecimal newTotal = new BigDecimal(oldTotal).subtract(new BigDecimal(1));
        info.setTotalPerson(newTotal + "人");

        //4.2 修改新增的person工程师类别在Info表中的数据
        String personTitle = pdsTestOrganizationPerson.getPersonTitle();

        //4.2.1 获取当前高级工程师的人数
        String seniorEngineer = pdsTestOrganizationInfo.getSeniorEngineer();
        int indexSeniorEngineer = seniorEngineer.indexOf("人");
        String oldSeniorEngineer = seniorEngineer.substring(0, indexSeniorEngineer);
        BigDecimal currentSeniorEngineer = new BigDecimal(oldSeniorEngineer);
        //4.2.2 获取当前助理工程师的人数
        String assistantEngineer = pdsTestOrganizationInfo.getAssistantEngineer();
        int indexAssistantEngineer = assistantEngineer.indexOf("人");
        String oldAssistantEngineer = assistantEngineer.substring(0, indexAssistantEngineer);
        BigDecimal currentAssistantEngineer = new BigDecimal(oldAssistantEngineer);
        //4.2.3 获取当前研究员高级工程师的人数
        String researcherSeniorEngineer = pdsTestOrganizationInfo.getResearcherSeniorEngineer();
        int indexResearcherSeniorEngineer = researcherSeniorEngineer.indexOf("人");
        String oldResearcherSeniorEngineer = researcherSeniorEngineer.substring(0, indexResearcherSeniorEngineer);
        BigDecimal currentResearcherSeniorEngineer = new BigDecimal(oldResearcherSeniorEngineer);
        //4.2.4 获取当前工程师的人数
        String engineer = pdsTestOrganizationInfo.getEngineer();
        int indexEngineer = engineer.indexOf("人");
        String oldEngineer = engineer.substring(0, indexEngineer);
        BigDecimal currentEngineer = new BigDecimal(oldEngineer);
        //4.2.5 获取当前技术员的人数
        String technician = pdsTestOrganizationInfo.getTechnician();
        int indexTechnician = technician.indexOf("人");
        String oldTechnician = technician.substring(0, indexTechnician);
        BigDecimal currentTechnician = new BigDecimal(oldTechnician);
        //4.2.6 获取当前其他的人数
        String otherPerson = pdsTestOrganizationInfo.getOtherPerson();
        int indexOtherPerson = otherPerson.indexOf("人");
        String oldOtherPerson = otherPerson.substring(0, indexOtherPerson);
        BigDecimal currentOtherPerson = new BigDecimal(oldOtherPerson);

        if ("2".equals(personTitle)) {
            currentSeniorEngineer = currentSeniorEngineer.subtract(new BigDecimal(1));
        } else if ("4".equals(personTitle)) {
            currentAssistantEngineer = currentAssistantEngineer.subtract(new BigDecimal(1));
        } else if ("1".equals(personTitle)) {
            currentResearcherSeniorEngineer = currentResearcherSeniorEngineer.subtract(new BigDecimal(1));
        } else if ("3".equals(personTitle)) {
            currentEngineer = currentEngineer.subtract(new BigDecimal(1));
        } else if ("5".equals(personTitle)) {
            currentTechnician = currentTechnician.subtract(new BigDecimal(1));
        } else if ("6".equals(personTitle)) {
            currentOtherPerson = currentOtherPerson.subtract(new BigDecimal(1));
        }

        if (newTotal.equals(new BigDecimal(0))) {
            info.setSeniorEngineer("0人 0.00%");
            info.setAssistantEngineer("0人 0.00%");
            info.setResearcherSeniorEngineer("0人 0.00%");
            info.setEngineer("0人 0.00%");
            info.setTechnician("0人 0.00%");
            info.setOtherPerson("0人 0.00%");
        } else {
            info.setSeniorEngineer(currentSeniorEngineer + "人 " + currentSeniorEngineer.multiply(new BigDecimal(100)).
                    divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
            info.setAssistantEngineer(currentAssistantEngineer + "人 " + currentAssistantEngineer.multiply(new BigDecimal(100)).
                    divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
            info.setResearcherSeniorEngineer(currentResearcherSeniorEngineer + "人 " + currentResearcherSeniorEngineer.multiply(new BigDecimal(100)).
                    divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
            info.setEngineer(currentEngineer + "人 " + currentEngineer.multiply(new BigDecimal(100)).
                    divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
            info.setTechnician(currentTechnician + "人 " + currentTechnician.multiply(new BigDecimal(100)).
                    divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
            info.setOtherPerson(currentOtherPerson + "人 " + currentOtherPerson.multiply(new BigDecimal(100)).
                    divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
        }

        //5. 将修改的数据在Info表中更新
        Date date = DateUtils.parseDate(DateUtils.getTime());
        info.setUpdateTime(date);
        pdsTestOrganizationInfoMapper.updateById(info);

        return flag1 & flag2 & flag3;
    }

    /**
     * 更新
     *
     * @param pdsTestOrganizationPerson
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updatePerson(PdsTestOrganizationPerson pdsTestOrganizationPerson) throws Exception {

        SysUser sysUser1 = SecurityUtils.getLoginUser().getUser();

        if(StringUtils.isNotEmpty(pdsTestOrganizationPerson.getPersonCode())&&
                !IdCardUtil.check(pdsTestOrganizationPerson.getPersonCode())){
            throw new Exception("身份证号不正确，请核实");
        }

        Long userId = pdsTestOrganizationPerson.getUserId();
        Long deptId = pdsTestOrganizationPerson.getDeptId();
        String roleIds = pdsTestOrganizationPerson.getPersonCategory();
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsTestOrganizationPerson.setUpdateTime(date);

        //判断用户表是否存在同手机号
        SysUser existUser = sysUserMapper.selectUserByUserName(pdsTestOrganizationPerson.getPhone());
        if (existUser != null && !String.valueOf(userId).equals(existUser.getUserId().toString())) {
            throw new Exception("修改失败，已存在相同手机号账户，请核实");
        }

        //判断本次修改是不是去掉了管理员权限，至于要有一个管理员账号
        List<Map> roles = sysRoleMapper.getAdminRoleByRoleKey(sysUser1.getDeptId(), ORG_ADMIN_ROLE_KEY);
        if (roles.size() == 1) {
            Map role = roles.get(0);
            //查看是否是当前修改的
            if (String.valueOf(userId).equals(role.get("userId").toString())) {
                if (!roleIds.contains(role.get("roleId").toString())) {
                    throw new Exception("至少需要有一个管理员");
                }
                if (StringUtils.isNotEmpty(pdsTestOrganizationPerson.getStatus()) && !"0".equals(pdsTestOrganizationPerson.getStatus())) {
                    throw new Exception("至少需要有一个管理员");
                }
            }
        }
        boolean flag1 = pdsTestOrganizationPersonMapper.updatePerson(pdsTestOrganizationPerson) > 0;
        SysUser sysUser = new SysUser();
        sysUser.setUserId(pdsTestOrganizationPerson.getUserId());
        sysUser.setDeptId(pdsTestOrganizationPerson.getDeptId());
        sysUser.setUserName(pdsTestOrganizationPerson.getPhone());
        sysUser.setNickName(pdsTestOrganizationPerson.getPersonName());
        sysUser.setEmail(pdsTestOrganizationPerson.getEmail());
        sysUser.setPhonenumber(pdsTestOrganizationPerson.getPhone());
        sysUser.setSex(pdsTestOrganizationPerson.getSex());
        sysUser.setStatus(pdsTestOrganizationPerson.getStatus());
        sysUser.setUpdateBy(sysUser1.getNickName());
        sysUser.setUpdateTime(date);
        sysUser.setRemark(pdsTestOrganizationPerson.getRemark());
        //更新sys_user表
        boolean flag2 = sysUserMapper.updateUser(sysUser) > 0;
        //更新sys_user_role表
        boolean flag3 = sysUserRoleMapper.deleteUserRoleByUserId(pdsTestOrganizationPerson.getUserId()) > 0;
        if (!StringUtils.isEmpty(roleIds)) {
            String[] roleIdArray = roleIds.split(",");
            List<SysUserRole> userRoleList = new ArrayList<>();
            for (String roleId : roleIdArray) {
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(pdsTestOrganizationPerson.getUserId());
                userRole.setRoleId(Long.parseLong(roleId));
                userRoleList.add(userRole);
            }
            sysUserRoleMapper.batchUserRole(userRoleList);
        }
        if (pdsTestOrganizationInfoMapper.selectByUserId(userId) != null){
            PdsTestOrganizationInfo info = new PdsTestOrganizationInfo();
            info.setContactName(pdsTestOrganizationPerson.getPersonName());
            info.setContactPhone(pdsTestOrganizationPerson.getPhone());
            pdsTestOrganizationInfoMapper.update(info, new QueryWrapper<PdsTestOrganizationInfo>().eq("user_id", userId));
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(deptId);
            sysDept.setLeader(pdsTestOrganizationPerson.getPersonName());
            sysDept.setPhone(pdsTestOrganizationPerson.getPhone());
            sysDeptMapper.updateDept(sysDept);
        }
        return flag1 & flag2 & flag3;
    }

    /**
     * 查询 -- 分页/条件：userId，status
     *
     * @param pdsTestOrganizationPerson
     * @return
     */
    @Override
    public List<PdsTestOrganizationPerson> selectPersonList(PdsTestOrganizationPerson pdsTestOrganizationPerson) {
        return pdsTestOrganizationPersonMapper.selectPersonList(pdsTestOrganizationPerson);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer importOrganizationPerson(List<OrganizationPersonExcel> list, Long organizationId) {
        int total = 0;
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        SysDept dept = sysUser.getDept();
        Date date = DateUtils.parseDate(DateUtils.getTime());
        for (OrganizationPersonExcel excel : list) {
            SysUser user = sysUserMapper.selectUserByUserName(excel.getPhone());
            String personCategory = excel.getPersonCategory();
            String personTitle = excel.getPersonTitle();
            String sex = excel.getSex();
            PdsTestOrganizationPerson person = new PdsTestOrganizationPerson();
            BeanUtils.copyProperties(excel, person);
            if (!Objects.isNull(personCategory) && !personCategory.isEmpty()) {
                String dictValue = dictDataMapper.selectDictValue("person_category", personCategory);
                person.setPersonCategory(dictValue);
            }
            if (!Objects.isNull(personTitle) && !personTitle.isEmpty()) {
                String dictValue = dictDataMapper.selectDictValue("person_title", personTitle);
                person.setPersonTitle(dictValue);
            }
            if (!Objects.isNull(sex) && !sex.isEmpty()) {
                String dictValue = dictDataMapper.selectDictValue("person_sex", sex);
                person.setSex(dictValue);
            }
            person.setStatus("1");
            person.setCreateTime(date);
            person.setOrganizationId(organizationId);
            total += pdsTestOrganizationPersonMapper.insert(person);
//            PdsTestOrganizationPerson person = new PdsTestOrganizationPerson();
//            person.setPersonName(excel.getPersonName());
//            person.setPersonCategory(excel.getPersonCategory());
        }
        return total;
    }

    @Override
    public List<PdsTestOrganizationPersonVO> getOrganizationPersonList(Map map) throws Exception {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysDept dept = sysUser.getDept();
//        String organizationId = map.get("organizationId") != null ? String.valueOf(map.get("organizationId")) : null;
        String personName = map.get("personName") != null ? String.valueOf(map.get("personName")) : null;
        String personCode = map.get("personCode") != null ? String.valueOf(map.get("personCode")) : null;
        String phone = map.get("phone") != null ? String.valueOf(map.get("phone")) : null;
        if ("1".equals(String.valueOf(dept.getType()))) {
            map.put("organizationId", dept.getOrganizationId());
        }

//        System.out.println(dept.getOrganizationId());
        List<PdsTestOrganizationPerson> personList = lambdaQuery().eq(PdsTestOrganizationPerson::getOrganizationId, map.get("organizationId"))
                .like(!StringUtils.isEmpty(personName), PdsTestOrganizationPerson::getPersonName, personName)
                .eq(!StringUtils.isEmpty(personCode), PdsTestOrganizationPerson::getPersonCode, personCode)
                .eq(!StringUtils.isEmpty(phone), PdsTestOrganizationPerson::getPhone, phone)
                .orderByAsc(PdsTestOrganizationPerson::getStatus)
                .orderByDesc(PdsTestOrganizationPerson::getCreateTime)
                .list();
        List<PdsTestOrganizationPersonVO> pdsTestOrganizationPersonVOList = new ArrayList<>();
        for (PdsTestOrganizationPerson person : personList) {
            String roleIds = person.getPersonCategory();
            SysUser user = sysUserMapper.selectUserById(person.getUserId());
            List<String> roleNameList = new ArrayList<>();
            if (!StringUtils.isEmpty(roleIds)) {
                String[] roleIdArray = roleIds.split(",");
//                List<Long> roleIdList = Arrays.stream(roleIdArray).map(roleId -> Long.parseLong(roleId)).collect(Collectors.toList());
                for (String roleId : roleIdArray) {
                    SysRole sysRole = sysRoleMapper.selectRoleById(Long.parseLong(roleId));
                    String roleName = sysRole.getRoleName();
                    roleNameList.add(roleName);
                }
                PdsTestOrganizationPersonVO pdsTestOrganizationPersonVO = new PdsTestOrganizationPersonVO();
//                BeanUtils.copyProperties(user, pdsTestOrganizationPersonVO);
                BeanUtils.copyProperties(person, pdsTestOrganizationPersonVO);
                pdsTestOrganizationPersonVO.setPersonName(user.getNickName());
                pdsTestOrganizationPersonVO.setPhone(user.getPhonenumber());
                pdsTestOrganizationPersonVO.setSex(user.getSex());
                pdsTestOrganizationPersonVO.setEmail(user.getEmail());
                pdsTestOrganizationPersonVO.setStatus(user.getStatus());
                pdsTestOrganizationPersonVO.setCreateTime(user.getCreateTime());
                pdsTestOrganizationPersonVO.setUpdateTime(user.getUpdateTime());
                pdsTestOrganizationPersonVO.setRoleName(roleNameList);
//                pdsTestOrganizationPersonVO.setPersonCategory(person.getPersonCategory());
//                pdsTestOrganizationPersonVO.setPersonCode(person.getPersonCode());
//                pdsTestOrganizationPersonVO.setPersonTitle(person.getPersonTitle());
//                pdsTestOrganizationPersonVO.setAge(person.getAge());
//                pdsTestOrganizationPersonVO.setTitleCertificate(person.getTitleCertificate());
//                pdsTestOrganizationPersonVO.setWorkCertificate(person.getWorkCertificate());
//                pdsTestOrganizationPersonVO.setRemark(person.getRemark());
                pdsTestOrganizationPersonVOList.add(pdsTestOrganizationPersonVO);
            }
        }
        return pdsTestOrganizationPersonVOList;
    }


    @Override
    public PdsTestOrganizationPersonVO getPersonInfo(Long id) {
        PdsTestOrganizationPerson person = lambdaQuery().eq(PdsTestOrganizationPerson::getId, id).one();
        String roleIds = person.getPersonCategory();
        List<String> roleNameList = new ArrayList<>();
        if (!StringUtils.isEmpty(roleIds)) {
            String[] roleIdArray = roleIds.split(",");
            for (String roleId : roleIdArray) {
                SysRole sysRole = sysRoleMapper.selectRoleById(Long.parseLong(roleId));
                String roleName = sysRole.getRoleName();
                roleNameList.add(roleName);
            }
        }
        PdsTestOrganizationPersonVO pdsTestOrganizationPersonVO = new PdsTestOrganizationPersonVO();
        BeanUtils.copyProperties(person, pdsTestOrganizationPersonVO);
        pdsTestOrganizationPersonVO.setRoleName(roleNameList);
        return pdsTestOrganizationPersonVO;
    }

    private void statisticianInformation(PdsTestOrganizationInfo pdsTestOrganizationInfo, PdsTestOrganizationPerson pdsTestOrganizationPerson) {
        PdsTestOrganizationInfo info = new PdsTestOrganizationInfo();
        info.setId(pdsTestOrganizationInfo.getId());

        //4.1 修改Info表的总人数
        String totalPerson = pdsTestOrganizationInfo.getTotalPerson();
        int totalIndex = totalPerson.indexOf("人");
        String oldTotal = totalPerson.substring(0, totalIndex);
        BigDecimal newTotal = new BigDecimal(oldTotal).add(new BigDecimal(1));
        info.setTotalPerson(newTotal + "人");

        //4.2 修改新增的person工程师类别在Info表中的数据
        String personTitle = pdsTestOrganizationPerson.getPersonTitle();

        //4.2.1 获取当前高级工程师的人数
        String seniorEngineer = pdsTestOrganizationInfo.getSeniorEngineer();
        int indexSeniorEngineer = seniorEngineer.indexOf("人");
        String oldSeniorEngineer = seniorEngineer.substring(0, indexSeniorEngineer);
        BigDecimal currentSeniorEngineer = new BigDecimal(oldSeniorEngineer);
        //4.2.2 获取当前助理工程师的人数
        String assistantEngineer = pdsTestOrganizationInfo.getAssistantEngineer();
        int indexAssistantEngineer = assistantEngineer.indexOf("人");
        String oldAssistantEngineer = assistantEngineer.substring(0, indexAssistantEngineer);
        BigDecimal currentAssistantEngineer = new BigDecimal(oldAssistantEngineer);
        //4.2.3 获取当前研究员高级工程师的人数
        String researcherSeniorEngineer = pdsTestOrganizationInfo.getResearcherSeniorEngineer();
        int indexResearcherSeniorEngineer = researcherSeniorEngineer.indexOf("人");
        String oldResearcherSeniorEngineer = researcherSeniorEngineer.substring(0, indexResearcherSeniorEngineer);
        BigDecimal currentResearcherSeniorEngineer = new BigDecimal(oldResearcherSeniorEngineer);
        //4.2.4 获取当前工程师的人数
        String engineer = pdsTestOrganizationInfo.getEngineer();
        int indexEngineer = engineer.indexOf("人");
        String oldEngineer = engineer.substring(0, indexEngineer);
        BigDecimal currentEngineer = new BigDecimal(oldEngineer);
        //4.2.5 获取当前技术员的人数
        String technician = pdsTestOrganizationInfo.getTechnician();
        int indexTechnician = technician.indexOf("人");
        String oldTechnician = technician.substring(0, indexTechnician);
        BigDecimal currentTechnician = new BigDecimal(oldTechnician);
        //4.2.6 获取当前其他的人数
        String otherPerson = pdsTestOrganizationInfo.getOtherPerson();
        int indexOtherPerson = otherPerson.indexOf("人");
        String oldOtherPerson = otherPerson.substring(0, indexOtherPerson);
        BigDecimal currentOtherPerson = new BigDecimal(oldOtherPerson);

        if ("2".equals(personTitle)) {
            currentSeniorEngineer = currentSeniorEngineer.add(new BigDecimal(1));
        } else if ("4".equals(personTitle)) {
            currentAssistantEngineer = currentAssistantEngineer.add(new BigDecimal(1));
        } else if ("1".equals(personTitle)) {
            currentResearcherSeniorEngineer = currentResearcherSeniorEngineer.add(new BigDecimal(1));
        } else if ("3".equals(personTitle)) {
            currentEngineer = currentEngineer.add(new BigDecimal(1));
        } else if ("5".equals(personTitle)) {
            currentTechnician = currentTechnician.add(new BigDecimal(1));
        } else if ("6".equals(personTitle)) {
            currentOtherPerson = currentOtherPerson.add(new BigDecimal(1));
        }

        info.setSeniorEngineer(currentSeniorEngineer + "人 " + currentSeniorEngineer.multiply(new BigDecimal(100)).
                divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
        info.setAssistantEngineer(currentAssistantEngineer + "人 " + currentAssistantEngineer.multiply(new BigDecimal(100)).
                divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
        info.setResearcherSeniorEngineer(currentResearcherSeniorEngineer + "人 " + currentResearcherSeniorEngineer.multiply(new BigDecimal(100)).
                divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
        info.setEngineer(currentEngineer + "人 " + currentEngineer.multiply(new BigDecimal(100)).
                divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
        info.setTechnician(currentTechnician + "人 " + currentTechnician.multiply(new BigDecimal(100)).
                divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");
        info.setOtherPerson(currentOtherPerson + "人 " + currentOtherPerson.multiply(new BigDecimal(100)).
                divide(newTotal, 2, BigDecimal.ROUND_HALF_UP) + "%");

        //5. 将修改的数据在Info表中更新
        Date date = DateUtils.parseDate(DateUtils.getTime());
        info.setUpdateTime(date);
        pdsTestOrganizationInfoMapper.updateById(info);
    }
}
